<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="车辆编号" prop="cidId">
        <el-input
          v-model="queryParams.carId"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆VIN" prop="vin">
        <el-input
          v-model="queryParams.vin"
          placeholder="请输入车辆VIN"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子围栏" prop="isFence">
        <el-input
          v-model="queryParams.isFence"
          placeholder="请输入电子围栏"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电机厂商" prop="machineId">
        <el-input
          v-model="queryParams.machineId"
          placeholder="请输入电机厂商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池厂商" prop="batteryId">
        <el-input
          v-model="queryParams.batteryId"
          placeholder="请输入电池厂商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电机编号" prop="machineNum">
        <el-input
          v-model="queryParams.machineNum"
          placeholder="请输入电机编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池编号" prop="batteryNum">
        <el-input
          v-model="queryParams.batteryNum"
          placeholder="请输入电池编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:carinfo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['car:carinfo:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['car:carinfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['car:carinfo:export']"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="carinfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="车辆编号" align="center" prop="cidId" /> -->
      <el-table-column label="车辆VIN" align="center" prop="vin" />
      <el-table-column label="车辆类型" align="center" prop="carName" />
      <el-table-column label="电子围栏" align="center" prop="isFence" />
      <el-table-column label="车辆状态" align="center" prop="isStatus">
        <template slot-scope="scope">
          {{ scope.row.isStatus == 1 ? "启动" : "未启动" }}
        </template>
      </el-table-column>
      <el-table-column label="电机厂商" align="center" prop="machineId" />
      <el-table-column label="电池厂商" align="center" prop="batteryId" />
      <el-table-column label="电机编号" align="center" prop="machineNum" />
      <el-table-column label="电池编号" align="center" prop="batteryNum" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['car:carinfo:edit']"
            >修改</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['car:carinfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车辆编号" prop="cidId">
          <el-input v-model="form.cidId" placeholder="请输入车辆编号" />
        </el-form-item>
        <el-form-item label="车辆VIN" prop="vin">
          <el-input v-model="form.vin" placeholder="请输入车辆VIN" />
        </el-form-item>
        <el-form-item label="电子围栏" prop="isFence">
          <el-select v-model="form.isFence" placeholder="请选择">
            <el-option
              v-for="item in options4"
              :key="item.id"
              :label="item.fenceName"
              :value="item.fenceName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车型" prop="isFence">
          <el-cascader :options="options" clearable v-model="form.carType">
          </el-cascader>
        </el-form-item>
        <el-form-item label="电机厂商" prop="machineId">
          <el-select v-model="form.machineId" placeholder="请选择">
            <el-option
              v-for="item in options1"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电池厂商" prop="batteryId">
          <el-select v-model="form.batteryId" placeholder="请选择">
            <el-option
              v-for="item in options2"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电机编号" prop="machineNum">
          <el-input v-model="form.machineNum" placeholder="请输入电机编号" />
        </el-form-item>
        <el-form-item label="电池编号" prop="batteryNum">
          <el-input v-model="form.batteryNum" placeholder="请输入电池编号" />
        </el-form-item>
        <el-form-item label="车辆状态" prop="status">
          <el-radio v-model="form.isStatus" label="1">启动</el-radio>
          <el-radio v-model="form.isStatus" label="2">未启动</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-hasPermi="['car:carinfo:add']"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCarinfo,
  getCarinfo,
  delCarinfo,
  addCarinfo,
  updateCarinfo,
  // getCartype,
  getCarBattery,
  getCarMachine,
  getCarType,
  getFence,
} from "@/api/car/carinfo";

export default {
  name: "Carinfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      carinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cidId: null,
        vin: null,
        carType: null,
        isFence: null,
        isStatus: null,
        machineId: null,
        batteryId: null,
        machineNum: null,
        batteryNum: null,
      },
      radio: "1",
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      id: 1,
      id1: 2,
      id2: 3,
      options: [],
      options1: [],
      options2: [],
      options4: [],
      value: null,
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleNodeClick(data) {
      console.log(data);
    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listCarinfo(this.queryParams).then((response) => {
        this.carinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      getCarType().then((resp) => {
        this.data = resp.rows;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cidId: null,
        vin: null,
        carType: null,
        isFence: null,
        isStatus: null,
        machineId: null,
        batteryId: null,
        machineNum: null,
        batteryNum: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆信息";
      getCarType().then((resp) => {
        this.options = resp.rows;
        console.log(resp);
      });
      getFence().then((resp) => {
        this.options4 = resp.rows;
      });
      getCarMachine(this.id1).then((resp) => {
        this.options1 = resp.rows;
      });
      getCarBattery(this.id2).then((resp) => {
        this.options2 = resp.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCarinfo(id).then((response) => {
        this.form = response.data;
        this.form.carType = JSON.parse(this.form.carType);
        this.open = true;
        this.title = "修改车辆信息";
      });
      getCarType().then((resp) => {
        this.options = resp.rows;
        console.log(resp);
      });
      getFence().then((resp) => {
        this.options4 = resp.rows;
      });
      getCarMachine(this.id1).then((resp) => {
        this.options1 = resp.rows;
      });
      getCarBattery(this.id2).then((resp) => {
        this.options2 = resp.rows;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.carType = JSON.stringify(this.form.carType);
            updateCarinfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.carType = JSON.stringify(this.form.carType);
            addCarinfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除车辆信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCarinfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "car/carinfo/export",
        {
          ...this.queryParams,
        },
        `carinfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
